Skip to content

Hashmap and Queue implementations#2437

Open
takchiks wants to merge 3 commits intosuper30admin:masterfrom
takchiks:master
Open

Hashmap and Queue implementations#2437
takchiks wants to merge 3 commits intosuper30admin:masterfrom
takchiks:master

Conversation

@takchiks
Copy link

@takchiks takchiks commented Feb 4, 2026

No description provided.

@super30admin
Copy link
Owner

Your solution for MyQueue is correct and efficient. The use of two stacks follows the standard approach, and the time complexity is amortized O(1) for pop and peek, which is optimal. The code is clean and easy to understand.

However, note that the problem you are solving is "Create Queue using Stacks", but you also included a solution for "MyHashMap" in the same file. This might be a mistake, as the problem does not require implementing a hash map. When submitting solutions, ensure that you only include the relevant code for the problem at hand.

For the MyQueue class:

  • You have correctly implemented all the methods.
  • The logic for transferring elements from pushStack to popStack only when necessary is efficient.
  • The empty method correctly checks both stacks.

One minor point: since the problem states that all calls to pop and peek are valid, you don't strictly need to return -1 in pop and peek when the queue is empty. However, it's good defensive programming to handle such cases. Alternatively, you could throw an exception (like NoSuchElementException) to indicate that the queue is empty, but the problem does not specify the behavior for invalid operations. Given the constraints, your approach is acceptable.

Overall, your solution is excellent. Just be careful to include only the required code for the problem.

@super30admin
Copy link
Owner

Strengths:

  • The solution correctly implements the queue using two stacks.
  • The code is clean and easy to understand.
  • The time and space complexities are optimal.

Areas for Improvement:

  • There is a minor issue: the problem states that all calls to pop and peek are valid, meaning the queue won't be empty when they are called. Therefore, returning -1 in pop and peek when the queue is empty is not strictly necessary. However, it is a safe practice to handle such cases. Since the problem guarantees valid calls, you could avoid the checks, but it's better to be safe.

  • In the pop method, you call popStack.pop() without checking if it's empty. But you have a condition that returns -1 if both stacks are empty. However, after transferring, if the popStack is still empty (which means the entire queue is empty), you return -1. But the problem states that all calls to pop and peek are valid, so you don't need to return -1. You can assume the queue is not empty when pop or peek is called. So you can simplify the code by removing the ternary checks and just do the operations.

For example, in pop:

public int pop() {
    if (popStack.isEmpty()) {
        while (!pushStack.isEmpty()) {
            popStack.push(pushStack.pop());
        }
    }
    return popStack.pop();
}

Similarly for peek:

public int peek() {
    if (popStack.isEmpty()) {
        while (!pushStack.isEmpty()) {
            popStack.push(pushStack.pop());
        }
    }
    return popStack.peek();
}

But your current code is safe for unexpected calls. Since the problem guarantees valid calls, it's acceptable to assume the stack is not empty.

Overall, the solution is excellent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants